<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>
<div id="app">
<h1>发布微博页面</h1>
    <input type="text" v-model="weibo.content" placeholder="说点儿什么...">
    <input type="button" value="发布" @click="send()">
    <el-upload
            limit="9"
            name="pic"
            action="/upload"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>

</div>
</body>
<!--引入Axios框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
   let v = new Vue({
        el: '#app',
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                weibo:{
                    content:"",
                    urls:""
                },
                fileList:[]
            }
        },
       methods:{
           handleRemove(file, fileList) {
               v.fileList = fileList;
               //得到图片的路径
             let url = file.response;
             //发请求删除图片
             axios.get("/remove?url="+url).then(function () {
                 console.log("服务器图片删除成功!");
             })

               console.log(file, fileList);
           },
           handlePictureCardPreview(file) {
               this.dialogImageUrl = file.url;
               this.dialogVisible = true;
           },
           send(){
               //从文件数组中得到多个图片的路径
               let arr = [];
               for (let file of v.fileList) {
                   //得到每一个图片的路径 添加到数组中
                    arr.push(file.response);
               }
               //把装着多个图片的数组转成字符串
               v.weibo.urls = arr.toString();
               axios.post("/weibo/insert",v.weibo).then(function (response) {
                   if (response.data==1){
                       alert("发布完成!");
                       location.href = "/";//回到首页
                   }else{
                       //删除图片
                       for (let file of v.fileList) {
                           axios.get("/remove?url="+file.response).then(function () {
                           })
                       }

                       alert("请先登录!")
                       location.href="/login.html";
                   }

               })
           },
           handleSuccess(response,file,fileList){
               v.fileList = fileList;
           }
       }
    })
</script>
</html>


<!--
weibo{
    content:"今天天气不错!",
    urls:"/a.jpg,/b.jpg,/c.jpg"
}


-->